<?php
declare (strict_types = 1);

namespace app\api\controller\express;

use app\admin\model\freight\ExpressCity;
use app\admin\model\freight\ExpressFree;
use app\api\controller\Base;
use app\common\service\freight\CityService;
use app\common\service\lib\ExpressLocusFree;
use app\common\service\lib\ExpressOneHundred;
use app\common\service\lib\kdzs\KdzsClient;
use app\common\service\lib\kdzs\KdzsLogisticsTraceMapGetRequest;
use app\common\service\lib\Logistics;
use app\common\service\freight\ExpressService;
use app\common\service\OrderService;
use app\common\service\store\GoodsService;
use app\common\service\user\AddressService;
use think\Request;

class Express extends Base
{

    /**
     * Author:Yingbo Sun
     * 快递物流接口
     * @return mixed|string|\think\response\Json
     */
    public function getExpressInquiry()
    {
        $express = $this->request->get('express');
        //快递公司 公司缩写
        $type = $this->request->get('type');
        if (empty($express) && empty($type)){
            return json(['error' => 400,'msg'=>'参数错误']);
        }
        $data = Logistics::logisticsInquire($express,$type);
        return $data;
    }

    /**
     * Author:Yingbo Sun
     * 快递运费
     * @return mixed
     */
    public function expressPrice()
    {
        //需要件数 运费模板id 地址id
        $params = $this->request->post();
        $addrId = $params['addr'];
        $pieces = $params['pieces'];
        $expressTemplateId = $params['expressTemplateId'];
        $res = ExpressService::getFreight($addrId,$pieces,$expressTemplateId);
        if ($res){
            $data = [
                'code' => 200,
                'message' => '运费获取成功',
                'data' => $res
            ];
        }else{
            $data = [
                'code' => 400,
                'message' => '获取失败',
                'data' => []
            ];
        }
        return $this->res($data);
    }

    public function getProvinces()
    {
      $data = CityService::sonArr();
      return $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     *  查询物流轨迹地图
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getExpressLocusFree()
    {
        $orderId = $this->request->post('order_id');
        if (empty($orderId)){
            return $this->apiFail('参数错误');
        }
        try {
            $skuId = OrderService::orderGoods($orderId);
            //获取商品订单信息
            $data = OrderService::orderDetails($orderId);
            //获取到用户邮寄地址
            $res = AddressService::addressOne($data->address_id);
            //获取商品发货地址
            $goodsAddress = GoodsService::goodsAddress($skuId);
        }catch (\Exception $e){
            return $this->apiFail('未知错误');
        }
        //用户详细地址
        $address = $res['data']['address'];
        //用户省市区地址
        $provinces = $res['data']['provinces'];
        //物流单号
        $cpCode = 'EMS';
        $mailNo = '9884063022744';
        $express = new ExpressLocusFree();
        $resp = $express->query($cpCode,$mailNo,'福建省泉州市','上海市奉贤区','上海市奉贤区五四公路3389号');
        return $this->apiSuccess('获取成功',$resp);
    }
}
